package wb.is.besti.edu.cd.javalibgame;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private int score = 0;
    private TextView tvScore, tvBestScore;
    private RelativeLayout root = null;
    private int isPlay = 0;
    private MediaPlayer mediaPlayer;
    private String string;
    //private Button btnNewGame;

    private GameView gameView;
    private AnimaLayer animLayer = null;
    public static final String SP_KEY_BEST_SCORE = "bestScore";
    private static MainActivity mainActivity = null;
    public MainActivity() {
        mainActivity = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initMediaPlayer();
        mediaPlayer.start();
        isPlay = 1;
        root = (RelativeLayout) findViewById(R.id.container);
        root.setBackgroundResource(R.drawable.background02);
        UnfoldButton unfoldButton = (UnfoldButton)findViewById(R.id.unfoldButton);
        unfoldButton.addElement(R.drawable.regame, R.color.colorPrimary, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.startGame();
            }
        });

        unfoldButton.addElement(R.drawable.ic_action_tiles_small, R.color.darkcyan, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (Config.LINES){
                    case 6:
                        Config.setLINES(4);
                        Config.CARD_WIDTH = (gameView.getWidth()-10)/Config.LINES;
                        gameView.clearCards();
                        break;
                    case 4:

                        Config.setLINES(6);
                        Config.CARD_WIDTH = (gameView.getWidth()-10)/Config.LINES;
                        gameView.clearCards();
                        break;
                    default:
                        break;
                }
            }
        });

        unfoldButton.addElement(R.drawable.ic_action_music_1, R.color.salmon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (isPlay){
                    case 0:
                        mediaPlayer.start();
                        isPlay = 1;
                        break;
                    case 1:
                        mediaPlayer.pause();
                        isPlay=0;
                        break;
                    default:
                        break;
                }

            }
        });
        unfoldButton.setmRotatable(true);
        unfoldButton.setmScale(1);
        unfoldButton.setLength(250);
        unfoldButton.setAngle(90);


        tvScore = (TextView) findViewById(R.id.tvScore);
        tvScore.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/type.ttf"));
        tvScore.setTextColor(0xffF08080);
        tvBestScore = (TextView) findViewById(R.id.tvBestScore);
        tvBestScore.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/type.ttf"));
        tvBestScore.setTextColor(0xff800000);
        gameView = (GameView) findViewById(R.id.gameView);

        animLayer = (AnimaLayer) findViewById(R.id.animLayer);

    }
    private void initMediaPlayer(){
        try {
            mediaPlayer = MediaPlayer.create(this,R.raw.sea);
            mediaPlayer.setLooping(true);
            mediaPlayer.prepare();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public static MainActivity getMainActivity() {
        return mainActivity;
    }

    public void onBackPressed() {

        new AlertDialog.Builder(this).setTitle("是否保存？")
                .setIcon(R.drawable.aaa)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        // 点击“确认”后的操作
                        string = String.valueOf(score);
                        Intent intent = new Intent();
                        if (score!=0){
                            intent.putExtra("text",string);
                            setResult(RESULT_OK, intent);
                        }
                        Config.setLINES(6);
                        finish();

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击“返回”后的操作,这里不设置没有任何操作
                        Intent intent = new Intent(MainActivity.this,First_page.class);
                        startActivity(intent);
                        finish();
                    }
                }).show();
    }

    public AnimaLayer getAnimLayer() {
        return animLayer;
    }
    public GameView getGameView(){
        GameView mygame = (GameView)findViewById(R.id.gameView);
        gameView = mygame;
        return gameView;
    }

    public void showBestScore(int s) {   //最高纪录的那个地方会显示出保存的最好的成绩
        tvBestScore.setText(s + "");
    }

    public int getBestScore() {
        return getPreferences(MODE_PRIVATE).getInt(SP_KEY_BEST_SCORE, 0);
    }

    public void saveBestScore(int s) {   //保存数据，将最大的值存到sd卡中
        SharedPreferences.Editor e = getPreferences(MODE_PRIVATE).edit();
        e.putInt(SP_KEY_BEST_SCORE, s);
        e.commit();
    }

    public void addScore(int s) {
        score += s;
        showScore();

        int maxScore = Math.max(score, getBestScore());
        saveBestScore(maxScore);
        showBestScore(maxScore);
    }

    public void showScore() {
        tvScore.setText(score + "");
    }

    public void clearScore() {
        score = 0;
        showScore();
    }
/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    */

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mediaPlayer.pause();
        isPlay=0;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            isPlay=1;
        }
    }
}
